﻿/*
Medion Info Display

Copyright 2010 Wolf Posdorfer,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Runtime.InteropServices;
using CoreAudioApi;

namespace MD8800.util
{
    public static class volume
    {
       private static MMDevice device;

        /// <summary>
        /// gibt die Lautstärke zwischen 0 und 7
        /// </summary>
        /// <returns>Lautstärke zwischen 0 und 7</returns>
        public static int getVolume()
        {
            MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
            device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
            int tbmaster = (int)(device.AudioEndpointVolume.MasterVolumeLevelScalar * 7);

            if (device.AudioEndpointVolume.Mute)
            {
                tbmaster = -1;
            }
 
            return tbmaster;

        }
        /// <summary>
        /// schreibt das speaker mute symbol
        /// </summary>
        /// <param name="volume">int</param>
        /// <param name="port">port</param>
        public static void mute(int volume, PortController port)
        {
            if (volume == -1)
            {
                port.write(opcodes.SPEAKER_MUTE + opcodes.BRIGHT_3);
                port.write(opcodes.SPEAKER + opcodes.BRIGHT_0);
            }
            else
            {
                port.write(opcodes.SPEAKER_MUTE + opcodes.BRIGHT_0);
                port.write(opcodes.SPEAKER + opcodes.BRIGHT_3);
            }

        }

        /// <summary>
        /// Setzt die Lautstärke entsprechend der Lautstärke
        /// </summary>
        /// <param name="port">der geöffnete Port</param>
        public static void setVolume(PortController port)
        {
            switch (getVolume())
            {

                case 1:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;
                case 2:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;
                case 3:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;
                case 4:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;
                case 5:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;
                case 6:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;
                case 7:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_3);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_3);
                    break;
                case 0:
                    mute(1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;
                default:
                    mute(-1, port);
                    port.write(opcodes.VOLUME_1 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_2 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_3 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_4 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_5 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_6 + opcodes.BRIGHT_0);
                    port.write(opcodes.VOLUME_7 + opcodes.BRIGHT_0);
                    break;

            }

        }
    
    }
}
